package com.homework.controller;

import com.homework.entity.Cat;
import com.homework.service.CatService;
import com.homework.service.CatServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//@WebServlet({"/deleteCat","/updateCat","/insertCat","/selectCat","/selectList"})
@Controller
public class CatController {

    @Autowired
    CatService catService;

    @RequestMapping("/insertCat.do")
    public String insertCat(Cat c){
        System.out.println("insertCat");
//        ModelAndView mav = new ModelAndView();
//        CatService catService = new CatServiceImpl();
        catService.insertCat(c);
        return "redirect:/selectList.do";
    }

    @RequestMapping("/selectList.do")
    public ModelAndView selectList(){
        System.out.println("selectList");
        ModelAndView mav = new ModelAndView();

//        CatService catService = new CatServiceImpl();
        List<Cat> list = catService.selectList();
        mav.addObject("list",list);
        mav.setViewName("cat");
//        return "forward:/views/cat.jsp";
        return mav;
    }

    @RequestMapping("/selectCat.do")
    public ModelAndView selectCat(Integer cno){
        System.out.println("selectCat");
        ModelAndView mav = new ModelAndView();
//        CatService catService = new CatServiceImpl();
        Cat cat = catService.selectCat(cno);
        mav.addObject("cat", cat);
        mav.setViewName("update");
        return mav;
    }

    @RequestMapping("/updateCat.do")
    public String updateCat(Cat c){
        System.out.println("updateCat");
//        ModelAndView mav = new ModelAndView();
//        CatService catService = new CatServiceImpl();
        catService.updateCat(c);
//        mav.setViewName("selectList");
        return  "redirect:/selectList.do";
    }

    @RequestMapping("/deleteCat.do")
    public String deleteCat(Integer cno){
        System.out.println("deleteCat");
//        ModelAndView mav = new ModelAndView();
//        CatService catService = new CatServiceImpl();
        catService.deleteCat(cno);
//        mav.setViewName("selectList");
        return  "redirect:/selectList.do";
    }

}
